<?php

namespace app\agent\controller;

use think\facade\Db;

use app\agent\model\Config as ConfigModel;
use app\agent\validate\Config as ConfigValidate;
use app\model\Config as BaseConfigModel;
/**
 * 系统配置
 */
class Config extends Controller
{
    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * 获取系统配置
     */
    public function get()
    {
        $model = new ConfigModel;
        return $model->get($this->agent['id']);
    }

    /**
     * 配置配置
     */
    public function set()
    {
        $data = input('post.');
        if (empty($data['scene'])) {
            return fail('参数有误');
        }
        $validate = new ConfigValidate;
        if (!$validate->scene($data['scene'])->check($data)) {
            return fail($validate->getError());
        }

        $model = new ConfigModel;
        $data['agent_id'] = $this->agent['id'];
        return $model->set($data);
    }

    /**
     * 上传支付证书
     */
    public function uploadCert()
    {
        $model = new ConfigModel;
        return $model->uploadCert();
    }

    /**
     *  查询全局副标题全局配置
     */
    public function getCouponSubtitle()
    {
        /**
         * @var BaseConfigModel $config
         */

        $config = BaseConfigModel::query()->where('type', 'coupon')->where('field', 'brand_subtitle')->find();
        if (!$config) {
            return fail('后台暂未配置，请联系技术');
        }
        return success('查询成功', ['brand_subtitle' => $config->value]);
    }
}
